package com.jsmile.cloud.authc.sys.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mall.api.exception.JsmileException;
import com.jsmile.mall.api.kit.FileKit;
import com.jsmile.mall.api.model.JsmileFile;
import com.jsmile.mall.file.constants.FileConstant;
import com.jsmile.mall.file.oss.OSSFactory;

import springfox.documentation.annotations.ApiIgnore;

@RestController
@ApiIgnore
public class UeditorController extends AuthcController {

    /**
     * desc: uedit后端配置
     */
    @RequestMapping("/ueditor/config")
    public JSONObject ueditor(HttpServletRequest request) {
        return JSON.parseObject(FileKit.readResourcesToJson("config.json"));
    }

    /**
     * desc: uedit图片上传
     */
    @RequestMapping("/ueditor/uploadImage")
    public Map<String, Object> uploadImage(HttpServletRequest request) {
        return upload(FileConstant.Ueditor.IMAGES, request);
    }

    /**
     * desc: uedit附件上传
     */
    @RequestMapping("/ueditor/uploadFile")
    public Map<String, Object> uploadFile(HttpServletRequest request) {
        return upload(FileConstant.Ueditor.FILES, request);
    }

    /**
     * desc: uedit视频上传
     */
    @RequestMapping("/ueditor/uploadVideo")
    public Map<String, Object> uploadVideo(HttpServletRequest request) {
        return upload(FileConstant.Ueditor.VIDEOS, request);
    }

    /**
     * desc: uedit附件列表
     */
    @RequestMapping("/ueditor/listFile")
    public Map<String, Object> listFile(HttpServletRequest request) {
        return uploadList(FileConstant.Ueditor.FILES, request);
    }

    /**
     * desc: uedit图片列表
     */
    @RequestMapping("/ueditor/listImage")
    public Map<String, Object> listImage(HttpServletRequest request) {
        return uploadList(FileConstant.Ueditor.IMAGES, request);
    }

    private Map<String, Object> uploadList(String filename, HttpServletRequest request) {
        Map<String, Object> m = Maps.newHashMap();
        m.put("state", "SUCCESS");
        m.put("total", request.getParameter("size"));
        m.put("start", request.getParameter("start"));

        List<Map<String, Object>> files = Lists.newArrayList();

        List<String> urls = OSSFactory.build().listFiles(filename);
        for (String tem : urls) {
            Map<String, Object> url = Maps.newHashMap();
            url.put("state", "SUCCESS");
            url.put("url", tem);
            files.add(url);
        }
        m.put("list", files);
        return m;
    }

    /**
     * desc: 文件上传
     */
    private Map<String, Object> upload(String filename, HttpServletRequest request) {
        try {
            Map<String, Object> m = Maps.newHashMap();
            List<JsmileFile> files = this.multiUpload(request, filename);
            m.put("state", "SUCCESS");
            if (files.size() > 0) {
                m.put("url", files.get(0).getUrl());
                m.put("title", files.get(0).getOriginalFilename());
            }
            m.put("original", request.getParameter("name"));
            m.put("type", request.getParameter("type"));
            m.put("size", request.getParameter("size"));
            return m;
        } catch (IOException e) {
            throw new JsmileException("文件上传异常!");
        }
    }
}
